﻿using System;
using System.Threading;

namespace System {
    /// <summary>
    /// 时间帮助类
    /// </summary>
    public class TimerHelper {
        /// <summary>
        /// 设置一个时间间隔的循环操作
        /// </summary>
        /// <param name="action">执行的操作</param>
        /// <param name="timeSpan">时间间隔</param>
        public static Timer SetInterval(Action action,TimeSpan timeSpan) {
            Timer threadTimer = new Timer((state => {
                action.Invoke();
            }),null,0,(long)timeSpan.TotalMilliseconds);


            return threadTimer;
        }

        /// <summary>
        /// 设置一个时间间隔的循环操作
        /// </summary>
        /// <param name="action">执行的操作</param>
        /// <param name="timeSpan">时间间隔</param>
        /// <param name="dely">延迟一段时间后再开始循环</param>
        public static Timer SetInterval(Action action,TimeSpan timeSpan,TimeSpan dely) {
            Timer threadTimer = new Timer((state => {
                action.Invoke();
            }),null,dely,timeSpan);

            return threadTimer;
        }
        /// <summary>
        /// 返回时间差
        /// </summary>
        public static string DateDiff(DateTime DateTime1,DateTime DateTime2) {
            string dateDiff = null;
            try {
                TimeSpan ts = DateTime2 - DateTime1;
                if(ts.Days >= 1) {
                    dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日";
                } else {
                    if(ts.Hours > 1) {
                        dateDiff = ts.Hours.ToString() + "小时前";
                    } else {
                        dateDiff = ts.Minutes.ToString() + "分钟前";
                    }
                }
            } catch { }
            return dateDiff;
        }
        public static string FormatDate(DateTime dateTime1,string dateMode) {
            switch(dateMode) {
                case "0":
                    return dateTime1.ToString("yyyy-MM-dd");
                case "1":
                    return dateTime1.ToString("yyyy-MM-dd HH:mm:ss");
                case "2":
                    return dateTime1.ToString("yyyy/MM/dd");
                case "3":
                    return dateTime1.ToString("yyyy年MM月dd日");
                case "4":
                    return dateTime1.ToString("MM-dd");
                case "5":
                    return dateTime1.ToString("MM/dd");
                case "6":
                    return dateTime1.ToString("MM月dd日");
                case "7":
                    return dateTime1.ToString("yyyy-MM");
                case "8":
                    return dateTime1.ToString("yyyy/MM");
                case "9":
                    return dateTime1.ToString("yyyy年MM月");
                default:
                    return dateTime1.ToString();
            }
        }
        /// <summary>
        /// 得到随机日期
        /// </summary>
        /// <param name="time1">起始日期</param>
        /// <param name="time2">结束日期</param>
        /// <returns>间隔日期之间的 随机日期</returns>
        public static DateTime GetRandomTime(DateTime time1,DateTime time2) {
            Random random = new Random();
            DateTime minTime = new DateTime();
            DateTime maxTime = new DateTime();
            System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);
           // 获取两个时间相隔的秒数
            double dTotalSecontds = ts.TotalSeconds;
            int iTotalSecontds = 0;
            if(dTotalSecontds > System.Int32.MaxValue) {
                iTotalSecontds = System.Int32.MaxValue;
            } else if(dTotalSecontds < System.Int32.MinValue) {
                iTotalSecontds = System.Int32.MinValue;
            } else {
                iTotalSecontds = (int)dTotalSecontds;
            }
            if(iTotalSecontds > 0) {
                minTime = time2;
                maxTime = time1;
            } else if(iTotalSecontds < 0) {
                minTime = time1;
                maxTime = time2;
            } else {
                return time1;
            }
            int maxValue = iTotalSecontds;
            if(iTotalSecontds <= System.Int32.MinValue)
                maxValue = System.Int32.MinValue + 1;
            int i = random.Next(System.Math.Abs(maxValue));
            return minTime.AddSeconds(i);
        }
        /// <summary>
        /// 获取日期对应的季度
        /// </summary>
        public static int GetQuarter(DateTime input) {
            if((input.Month % 3) > 0)
                return (input.Month / 3 + 1);
            else
                return (input.Month / 3);
        }
        /// <summary>
        /// 返回每月的第一天和最后一天
        /// </summary>
        public static void ReturnDateFormat(int month,out string firstDay,out string lastDay) {
            int year = DateTime.Now.Year + month / 12;
            if(month != 12) {
                month = month % 12;
            }
            switch(month) {
                case 1:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
                    break;
                case 2:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    if(DateTime.IsLeapYear(DateTime.Now.Year))
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-29");
                    else
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-28");
                    break;
                case 3:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    lastDay = DateTime.Now.ToString("yyyy-0" + month + "-31");
                    break;
                case 4:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
                    break;
                case 5:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
                    break;
                case 6:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
                    break;
                case 7:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
                    break;
                case 8:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
                    break;
                case 9:
                    firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
                    break;
                case 10:
                    firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-" + month + "-31");
                    break;
                case 11:
                    firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-" + month + "-30");
                    break;
                default:
                    firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
                    lastDay = DateTime.Now.ToString(year + "-" + month + "-31");
                    break;
            }
        }
    }
}
